<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/funcionesExternas.js" language="JavaScript"> </script>
<script>
function login() {
		var us=$('#username').val();
		var pas=md5($('#password').val());
		if (window.XMLHttpRequest) 
		{ 
   		 	var request = new XMLHttpRequest();     // Firefox, Safari, ...
		} 
		else if (window.ActiveXObject)   // Internet Explorer 
		{
    		var request = new ActiveXObject("Microsoft.XMLHTTP");
		}
  		var peticion_str = '/login/'+us+'/'+pas;
  		request.open('GET', peticion_str , true);
  		request.onreadystatechange= function(){
			if ( request.readyState == 4 && request.status == 200) {
				if(request.responseText!==""){
					var json;
					eval ( 'json = '+ request.responseText );
					peticion_str = '/inicSesion';
					request.onreadystatechange= function(){
						if ( request.readyState == 4 && request.status == 200) {
							if(request.responseText=='ok'){
								$(location).attr('href','pagPrincipal'); 
							}
						};
					};
					request.open('POST', peticion_str , true);
					request.setRequestHeader('Content-Type', 'application/json');
					request.send(JSON.stringify(json));
				}else{
					alert('El usuario no existe');
				}
  			}
		};
		request.send(null);
	}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Bienvenido, logueate</title>

<!-- Cargamos hojas de estilo y ficheros JavaScript -->

<link href="css/estilosFormulario.css" rel="stylesheet" type="text/css">
<link href="css/estilosLogin.css" rel="stylesheet" type="text/css">
</head>

<body>
<div class="container"> 
  <!--/ freshdesignweb top bar -->
  <div id="cabecera">
    <div id="titulo"> FACE
      <div id="torcido">the</div>
      CHALLENGE </div>
  </div>
  <div  class="form" id="home-login">
    <form id="loginform"  name="loginform">
      <p class="contact">
        <label for="username">Nombre de usuario</label>
      </p>
      <input id="username" name="username" placeholder="Nick" required="" type="text">
      <p class="contact">
        <label for="password">Contraseña</label>
      </p>
      <input type="password" id="password" name="password" required="">
      <br>
      <br>
      <input class="buttom" name="submit" id="submit" value="¡Soy un valiente!" type="button" onClick="login()">
      </a>
    </form>
  </div>
  <div  class="form" id="home-registro">
    <form id="regnuevoform"  name="regnuevoform">
      <h4>Registrate y comienza tu reto</h4>
      <a href="pagRegistro">
      <input class="buttom" name="submit" id="regbuttom" value="Registrarse">
      </a>
    </form>
  </div>
</div>
</body>
</html>
